package org.ifdb.persistency.entity;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.Query;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.ifdb.persistency.PMF;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Country {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key         id;

    @Persistent
    private String      name;

    @Persistent
    @Element(dependent = "true")
    private List<State> states = new ArrayList<State>();

    public Key getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<State> getStates() {
        return states;
    }

    public boolean addState(State state) {
        return states.add(state);
    }

    public boolean removeState(State state) {
        return states.remove(state);
    }

    public State getStateByName(String name) {
        State state = null;
        for (State s : states) {
            if (s.getName().equals(name)) {
                state = s;
            }
        }
        return state;
    }
    
    public static final boolean exists(String countryName){
        Country c = new Country();
        c.setName(countryName);
        return Country.exists(c);
    }
    
    @SuppressWarnings("unchecked")
    public static final boolean exists(Country country){
        boolean exists = false;
        Query query = null ;
        try { 
             query = PMF.get().getPersistenceManager().newQuery(Country.class);     
             query.setFilter("name == countryName");      
             query.declareParameters("String countryName"); 
        } catch(Exception e){}
          try {         
              List<Country> results = (List<Country>) query.execute(country.getName());         
              if (results.iterator().hasNext()) {
                  for (Country c : results) {
                      c.getId();
                      exists = true;
                      break;
                  }
              } else {             
                  exists = false;
              }     
          } finally {        
          
              query.closeAll();     
          }
        return exists;
        
    }

   
    public static final Country getCountryByName(String countryName){
        Country returnCountry = null;
        Query query = null ;
        try { 
             query = PMF.get().getPersistenceManager().newQuery(Country.class);     
             query.setFilter("name == countryName");      
             query.declareParameters("String countryName"); 
        } catch(Exception e){}
          try {         
              List<Country> results = (List<Country>) query.execute(countryName);         
              if (results.iterator().hasNext()) {
                  for (Country c : results) {
                      returnCountry = c;
                      break;
                  }
              } else {             
                  returnCountry = null;
              }     
          } finally {        
          
              query.closeAll();     
          }
        return returnCountry;
        
    }
    
}
